<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@tag pageEncoding="UTF-8" isELIgnored="false"  %>
<%@attribute name="label"%>
<%@attribute name="placeholder"%>
<%--type 1:允许选择门店(门店下全部)和仓库 2：只能选择仓库--%>
<%@attribute name="type"%>
<%@attribute name="showDefault"%>
<%@attribute name="formFilter"%>


<div class="layui-inline selectHouseDiv">
    <input type="hidden" name="storeId">
    <input type="hidden" name="houseId">
    <div class="label">${label}</div>
    <div style="display: inline-block;width: 160px;">
        <cascader id="selectHouse"></cascader>
    </div>
    <script>
        let oldHouseId = "";
        let oldStoreId = "";

        let option = {
            elem: '#selectHouse'
            ,placeholder:"${placeholder}"
            ,width:160
            , value: "0"
            ,clear:true
            , getChildren: function (value, callback) {
                var data = [];
                //表示是门店
                if (value == "0"){
                    get("${baseUrl}organization/store/getStores.action", function (res) {
                        if (res.code == 1){
                            data = res.data;
                            for (var i in data) {
                                data[i].value = data[i].id;
                                data[i].label = data[i].name;
                                delete data[i].id;
                                delete data[i].name;
                                data[i].hasChild = true;
                            }
                            callback(data);
                        }
                    });
                } else {
                    //表示是仓库
                    get("${baseUrl}organization/house/getHouseByStore.action?storeId="+value, function (res) {
                        if (res.code == 1){
                            if ("${type}" == "1") {
                                data.push({value:"", label:"全部"});
                            }
                            $.each(res.data, function (i,d) {
                                data.push({value:d.id, label:d.name, disabled:isTrue(d.locking)});
                            });

                            callback(data);
                        }
                    });
                }
            }
        };

        layui.use(['jquery','ajaxCascader'],function() {
            var $ = layui.jquery;
            var cascader = layui.ajaxCascader;

            <c:if test="${showDefault == 1}">
            get("${baseUrl}organization/house/getDefault.action", function (res) {
                if (res.code == 1){
                    if (!res.data.locking) {
                        let chooseData = [];
                        chooseData.push(res.data.storeId);
                        chooseData.push(res.data.id);
                        option.chooseData = chooseData;

                        // Ajax传参模式
                        cascader.load(option);
                        $("[name='houseId']").val(res.data.id);
                    }
                }
            });
            </c:if>

            // Ajax传参模式
            cascader.load(option);

            // 选择器点击事件的监听
            cascader.on('click', '#selectHouse', function(data){
                oldHouseId = $("[name='houseId']").val();
                if (data.disabled) {
                    errTip("该仓库正在盘点，解锁后才能进行业务操作！");
                    clearHouse();
                    return;
                }
                $("[name='houseId']").val(data.value);

                if (oldHouseId!="" && oldHouseId != data.value) {
                    if (houseChange != undefined) {
                        houseChange(data.value, oldHouseId);
                    }
                }
            });

            $(".cascader-clear").click(function () {
                $("[name='houseId']").val("");
                houseChange("",oldHouseId);
            });
        });
        
        
        function clearHouse() {
            $(".cascader-clear").click();
        }
    </script>
</div>